﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataBaseAccessLib;
using BusinessModel.EntityModel;
using System.Data;

namespace BusinessModel
{
    public class LogHelper
    {
        /// <summary>
        /// 日志操作类


        /// </summary>
        SQLScript sqlScript = new SQLScript();

        /// <summary>
        /// 增加日志信息
        /// </summary>
        /// <param name="logInfo">日志信息实体</param>
        ///  <param name="page">当前页面 值统一为this</param>
        /// <returns></returns>
        public bool AddLog(LogInfo logInfo, System.Web.UI.Page page)
        {
            DBType dbType = OConfig.SysDBType;
            IDBAccess DB = DBFactory.GetDBAccess(dbType, OConfig.DBConnection);
            int isSuccess = 0;
            string sqlCommand = " insert into ts_log (logID,userguid,logDate,logModal,logContent,logIP,logDemo) values ('" + OConfig.GetGUID() + "','" + logInfo.UserGUID + "',  '" + DateTime.Now.ToString() + "', '" + logInfo.LogModal + "','" + logInfo.LogContent + "','" + page.Request.UserHostAddress.ToString() + "','" + logInfo.LogDemo + "') ";
            isSuccess = DB.ExecuteNonQuery(sqlCommand, CommandType.Text, null);
            if (isSuccess > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 增加一条数据


        /// </summary>
        public bool AddLog(LogInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            DBType dbType = OConfig.SysDBType;
            IDBAccess DB = DBFactory.GetDBAccess(dbType, OConfig.DBConnection);
            if (model.LogID != null)
            {
                strSql1.Append("LogID,");
                strSql2.Append("'" + model.LogID + "',");
            }
            if (model.UserGUID != null)
            {
                strSql1.Append("UserGUID,");
                strSql2.Append("'" + model.UserGUID + "',");
            }
            if (model.LogDate != null)
            {
                strSql1.Append("LogDate,");
                strSql2.Append("'" + model.LogDate + "',");
            }
            if (model.LogModal != null)
            {
                strSql1.Append("LogModal,");
                strSql2.Append("'" + model.LogModal + "',");
            }
            if (model.LogContent != null)
            {
                strSql1.Append("LogContent,");
                strSql2.Append("'" + model.LogContent + "',");
            }
            if (model.LogIP != null)
            {
                strSql1.Append("LogIP,");
                strSql2.Append("'" + model.LogIP + "',");
            }
            if (model.LogDemo != null)
            {
                strSql1.Append("LogDemo,");
                strSql2.Append("'" + model.LogDemo + "',");
            }
            if (model.LogSQL != null)
            {
                strSql1.Append("LogSQL,");
                strSql2.Append("'" + model.LogSQL + "',");
            }
            strSql.Append("insert into ts_log(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int isSuccess = DB.ExecuteNonQuery(strSql.ToString(), CommandType.Text, null);
            if (isSuccess > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>      
        public void DeleteLog(string logID)
        {
            DBType dbType = OConfig.SysDBType;
            IDBAccess DB = DBFactory.GetDBAccess(dbType, OConfig.DBConnection);
            string sqlCommand = " delete ts_log where logID='" + logID + "' ";
            DB.ExecuteDataSet(sqlCommand, CommandType.Text, null);
        }

        /// <summary>
        /// 删除信息
        /// </summary>      
        public void DeleteAll()
        {
            DBType dbType = OConfig.SysDBType;
            IDBAccess DB = DBFactory.GetDBAccess(dbType, OConfig.DBConnection);
            string sqlCommand = " delete ts_log  ";
            DB.ExecuteDataSet(sqlCommand, CommandType.Text, null);
        }
        /// <summary>
        /// 获取记录
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public DataTable GetInformation()
        {
            DBType dbType = OConfig.SysDBType;
            IDBAccess DB = DBFactory.GetDBAccess(dbType, OConfig.DBConnection);
            string strsql = @"select * from ts_log  where  logdate in (select min(logdate) from ts_log where  logmodal='用户登录' and  
                             convert(char(10),logdate,120)=(Select Convert(varchar(10),getDate(),120)) group by userguid)";
            return DB.ExecuteDataTable(strsql, CommandType.Text, null);
        }
        /// <summary>
        /// 获取所有信息


        /// </summary>
        /// <returns></returns>
        public DataTable GetLog(int pageSize, int currentPage, Wuqi.Webdiyer.AspNetPager pager, string sortExpression, string sortDirection, string condition)
        {
            int totalPage = 0, totalRow = 0;
            try
            {
                IDBAccess DB = DBFactory.GetDBAccess(OConfig.SysDBType, OConfig.DBConnection);
                int startIndex, endIndex;
                string strCol = " b.username,a.* ";
                string strTable = " ts_log as a join ts_user as b on a.userguid=b.userguid  ";
                OConfig.GetStartRowNum(strTable, condition, currentPage, pageSize, out startIndex, out endIndex, out totalPage, out totalRow);
                string sqlCommand = sqlScript.QueryByDividePage(strTable, condition, strCol, sortExpression + " " + sortDirection, startIndex, endIndex);
                DataTable table = DB.ExecuteDataTable(sqlCommand, CommandType.Text, null);
                pager.RecordCount = totalRow;
                if (currentPage > pager.PageCount)
                {
                    currentPage = pager.PageCount;
                }
                if (currentPage == 0)
                {
                    currentPage = 1;
                }
                pager.CurrentPageIndex = currentPage;
                return table;
            }
            catch (Exception exp)
            {
                return null;
            }
            finally
            {
                pager.CustomInfoHTML = "共<font color=red>" + pager.RecordCount + "</font>条数据 当前<font color=red>" + pager.CurrentPageIndex + "/" + pager.PageCount + "</font>页";
            }
        }

        /// <summary>
        /// 获取所有信息


        /// </summary>
        /// <returns></returns>
        public DataTable GetTheLog(int pageSize, int currentPage, Wuqi.Webdiyer.AspNetPager pager, string sortExpression, string sortDirection, string condition)
        {
            int totalPage = 0, totalRow = 0;
            try
            {
                IDBAccess DB = DBFactory.GetDBAccess(OConfig.SysDBType, OConfig.DBConnection);
                int startIndex, endIndex;
                string strCol = " b.username,a.* ";
                string strTable = " ts_log as a join ts_user as b on a.userguid=b.userguid  ";
                OConfig.GetStartRowNum(strTable, condition, currentPage, pageSize, out startIndex, out endIndex, out totalPage, out totalRow);
                string sqlCommand = sqlScript.QueryByDividePage(strTable, condition, strCol, sortExpression + " " + sortDirection, startIndex, endIndex);
                DataTable table = DB.ExecuteDataTable(sqlCommand, CommandType.Text, null);
                pager.RecordCount = totalRow;
                if (currentPage > pager.PageCount)
                {
                    currentPage = pager.PageCount;
                }
                if (currentPage == 0)
                {
                    currentPage = 1;
                }
                pager.CurrentPageIndex = currentPage;
                return table;
            }
            catch (Exception exp)
            {
                return null;
            }
            finally
            {
                pager.CustomInfoHTML = "共<font color=red>" + pager.RecordCount + "</font>条数据 当前<font color=red>" + pager.CurrentPageIndex + "/" + pager.PageCount + "</font>页";
            }
        }
    }
}
